from fastapi import FastAPI,Form,HTTPException
import pymysql
app=FastAPI()
@app.post("/update")
def change(username:str=Form(...),password:str=Form(...)):
    global conn, cursor
    try:
        # 修改代码
        conn = pymysql.Connection(user="root",password="Aa1234567.",database="nm")
        # 创建游标对象
        cursor = conn.cursor()
        cursor.execute("select * from user where username=%s",(username,))
        a = cursor.fetchone()
        if a is None:
            # 创建失败
            raise HTTPException(400, "修改失败")
        else:
            # 创建成功
            return {"msg": "修改成功"}
        # 执行SQL语句
        cursor.execute("update user set password = %s where username = %s",
                       (username,password))
        # 提交语句
        conn.commit()
    except Exception as e:
        print(e)
        raise
    finally:
        # 关闭资源
        cursor.close()
        conn.close()